﻿using System;
using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Controller.Htmls;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class CreateController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }
        private static readonly ILog logger = LogManager.GetLogger(typeof(CreateController));

        public CreateController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet]
        public virtual void CreateIndex()
        {
            SetPublic();
            setpublic();
            target(CreateIndexDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var is_alonelng = configsService.GetValue("is_alonelng").ToInt();
            set("is_alonelng", is_alonelng);
            set("home_lng", configsService.GetValue("home_lng"));
            var lnglist = Lng.find(" isopen =:i order by pid asc,id asc").set("i",1).list();
            bind("lnglist", lnglist);
            set("htmldir", configsService.GetValue("file_htmldir"));
            set("entrance", configsService.GetValue("entrance_file"));
            set("fileex", configsService.GetValue("file_fileex"));
            set("getlngdir", to(GetLngDir));
        }

        [HttpPost]
        public virtual void CreateIndexDo()
        {
            var lng = ctx.Post("lng");
            HtmlMaker.GetHome().Process(lng);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void GetLngDir()
        {
            var lng = ctx.Post("lng");
            ctx.web.ResponseWrite(lng);
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void CreateType()
        {
            SetPublic();
            setpublic();
            target(CreateTypeDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var is_alonelng = configsService.GetValue("is_alonelng").ToInt();
            set("is_alonelng", is_alonelng);
            set("home_lng", configsService.GetValue("home_lng"));
            var lnglist = Lng.find(" isopen =:i order by pid asc,id asc").set("i", 1).list();
            bind("lnglist", lnglist);
            var mlist = Model.find(" isclass = 1").list();
            bind("mlist", mlist);

        }

        [HttpPost]
        public virtual void CreateTypeDo()
        {

        }

        [HttpGet]
        public virtual void ScModelList()
        {
            var lng = ctx.Get("lng");
            var mlist = Model.find(" isclass = 1").list();
            var builder = new MyStringBuilder();
            builder.Append("<option value=\"0\">" + lang("createmain_add_mid_option") + "</option>");
            foreach (var row in mlist)
            {
                builder.Append("<option value=\"" + row.Id + "\">" + row.modelname + "</option>");
            }
            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void ScTypeList()
        {
            var lng = ctx.Get("lng");
            var islinkd = ctx.Get("islinkd");
            var optiontitle = ctx.Get("optiontitle");
            var mid = ctx.GetInt("mid");
            var builder = new MyStringBuilder();
            builder.Append("<option value=\"0\">" + optiontitle + "</option>");
            var typelist = TypeList.find(" topid=0 and isclass = 1 and lng=:l order by pid asc,id asc").set("l", lng).list();
            var tlist = new List<TypeList>();
            foreach (var row in typelist)
            {
                if (row.mid == mid)
                {
                    tlist.Add(row);
                    getmtypelist(ref tlist, row.Child, mid);
                }
                else
                {
                    getmtypelist(ref tlist, row.Child, mid);
                }
            }
            foreach (var row in tlist)
            {
                var level = String.Empty;
                for (var i = 0; i < row.level; i++)
                {
                    level += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                level += "├─&nbsp;";
                builder.Append("<option value=\"" + row.Id + "\">" + level + row.typename + "</option>");
            }
            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        protected List<TypeList> getmtypelist(ref List<TypeList> list, List<TypeList> clist , Int32 mid)
        {
            foreach (var row in clist)
            {
                if(row.mid == mid)
                {
                    list.Add(row);
                    getmtypelist(ref list, row.Child, mid);
                }
                else
                {
                    getmtypelist(ref list, row.Child, mid);
                }
            }
            return list;

        }

        [HttpGet]
        public virtual void CreateTypeWindow()
        {
            var lng = ctx.Get("lng");
            var mid = ctx.Get("mid");
            var tid = ctx.Get("tid");
            var creattype = ctx.Get("creattype");
            var pnumber = ctx.Get("pnumber");
            var digheight = ctx.Get("digheight");
            set("is_html", configsService.GetValue("is_html"));
            set("lng", lng);
            set("mid", mid);
            set("tid", tid);
            set("creattype", creattype);
            set("pnumber", pnumber);
            set("digheight", digheight);
            set("time", DateTime.Now);


        }

        [HttpGet, HttpPost]
        public virtual void Statype()
        {
            var clas = ctx.Get("class");
            var lng = ctx.Post("lng");
            var mid = ctx.PostInt("mid");
            var tid = ctx.PostInt("tid");
            var creattype = ctx.PostInt("creattype");
            var pnumber = ctx.PostInt("pnumber");
            var builder = new MyStringBuilder();

            if (creattype == 0)
            {
                builder.Append("{\"num\":\"1\",\"typelist\":\"" + tid + "\",\"mid\":\"" + mid + "\",\"lng\":\"" + lng + "\"}");
            }
            else
            {
                var condition = " topid=0 and isclass = 1 and lng=:l order by pid asc,id asc";
                if (tid > 0)
                {
                    condition = " isclass = 1 and lng=:l and Id = " + tid + " order by pid asc,id asc";
                }
                var typelist = TypeList.find(condition).set("l", lng).list();
                var tlist = new List<TypeList>();
                foreach (var row in typelist)
                {
                    if (row.mid == mid)
                    {
                        tlist.Add(row);
                        getmtypelist(ref tlist, row.Child, mid);
                    }
                    else
                    {
                        getmtypelist(ref tlist, row.Child, mid);
                    }
                }
                var str = String.Empty;
                foreach (var row in tlist)
                {
                    str += row.Id + ",";
                }
                str = str.TrimEnd(',');
                builder.Append("{\"num\":\"" + tlist.Count + "\",\"typelist\":\"" + str + "\",\"mid\":\"" + mid + "\",\"lng\":\"" + lng + "\"}");
            }

            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void CreatTypeSave()
        {
            var clas = ctx.Get("class");
            var lng = ctx.Post("lng");
            var mid = ctx.PostInt("mid");
            var tid = ctx.PostInt("tid");
            var creattype = ctx.PostInt("creattype");
            var pnumber = ctx.PostInt("pnumber");
            var str = HtmlMaker.GetList().ProcessAll(lng, tid, mid, creattype, pnumber);
            ctx.web.ResponseWrite(str);
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void CreateDoc()
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var is_alonelng = configsService.GetValue("is_alonelng").ToInt();
            set("is_alonelng", is_alonelng);
            set("home_lng", configsService.GetValue("home_lng"));
            var lnglist = Lng.find(" isopen =:i order by pid asc,id asc").set("i", 1).list();
            bind("lnglist", lnglist);
            var mlist = Model.find(" isclass = 1 and isbase = 0").list();
            bind("mlist", mlist);
        }

        [HttpGet, HttpPost]
        public virtual void CreateDocWindow()
        {
            var lng = ctx.Get("lng");
            var mid = ctx.Get("mid");
            var tid = ctx.Get("tid");
            var startid = ctx.Get("startid");
            var endid = ctx.Get("endid");
            var digheight = ctx.Get("digheight");
            set("is_html", configsService.GetValue("is_html"));
            set("lng", lng);
            set("mid", mid);
            set("tid", tid);
            set("startid", startid);
            set("endid", endid);
            set("digheight", digheight);
        }

        [HttpPost]
        public virtual void DocNum()
        {
            var lng = ctx.Post("lng");
            var mid = ctx.Post("mid");
            var tid = ctx.Post("tid");
            var startid = ctx.Post("startid");
            var endid = ctx.Post("endid");

            var count = Document.count(" tid in (" + tid + ") and ishtml = 1");

            ctx.web.ResponseWrite(count.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void CreatDocSave()
        {
            var lng = ctx.Post("lng");
            var mid = ctx.PostInt("mid");
            var tid = ctx.Post("tid");
            var startid = ctx.PostInt("startid");
            var endid = ctx.PostInt("endid");
            var limit = ctx.PostInt("limit");
            var limitnummax = ctx.PostInt("limitnummax");

            CurrentRequest.setCurrentPage(limit / 10);

            var list = Document.findPage(" tid in (" + tid + ") and ishtml = 1 order by id asc", 10).Results;

            makeDetail(list, lng);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();

        }

        private void makeDetail(List<Document> list, String lng)
        {
            foreach (var post in list)
            {
                HtmlMaker.GetRead().Process(post, lng);
                logger.Info("make detail html, postId=" + post.Id);
            }

        }

    }
}
